layui.use(['form', 'tree', 'element', 'table', 'laytpl', 'treeGrid', 'layer', 'jquery'], function () {
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;

    //回显数据
    var request = GetRequest();

    /*用户标签*/
    var userTagChart = echarts.init(document.getElementById('main'));
    userTagChart.setOption({
        toolbox: {
            feature: {
                dataView: {show: false, readOnly: false},
                // magicType: {show: true, type: ['line', 'bar']},
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        series: [{
            type: 'wordCloud',
            shape: 'circle',
            // maskImage: maskImage,
            left: 'center',
            top: 'center',
            width: '90%',
            height: '90%',
            right: null,
            bottom: null,
            sizeRange: [8, 40],
            rotationRange: [-90, 90],
            rotationStep: 45,
            gridSize: 2,
            drawOutOfBound: false,
            textStyle: {
                normal: {
                    fontFamily: 'sans-serif',
                    fontWeight: 'bold',
                    // Color can be a callback function or a color string
                    color: function () {
                        // Random color
                        return 'rgb(' + [
                            Math.round(Math.random() * 160),
                            Math.round(Math.random() * 160),
                            Math.round(Math.random() * 160)
                        ].join(',') + ')';
                    }
                },
                emphasis: {
                    shadowBlur: 10,
                    shadowColor: '#333'
                }
            },
            data: []

        }]
    });
    $.get("/api/agt_user_tag/test?userId="+request.id).done(function (resp) {
        userTagChart.setOption({
            series: {data: JSON.parse(JSON.stringify(resp).replace(/tagName/g, "name").replace(/weight/g, "value"))}
        });
    });
    userTagChart.on('click', function (params) {
        console.log(params.name);

    });

    /*用户行为图表*/
    var actionChartOption = {
        title: {
            text: '用户行为',//主标题文本
            subtext: '展示用户进一周内行为次数',//副标题文本
            show: false,//标题是否显示 默认为true
            x: 'center'//标题的位置 默认是left
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                crossStyle: {
                    color: '#999'
                }
            }
        },
        toolbox: {
            feature: {
                dataView: {show: false, readOnly: false},
                magicType: {show: true, type: ['line', 'bar']},
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        legend: {
            data: []
        },
        xAxis: [
            {
                type: 'category',
                data: [],
                axisPointer: {
                    type: 'shadow'
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                name: '次数',
                min: 0,
                // max: 50,
                minInterval: 1,
                interval: 5,
                axisLabel: {
                    formatter: '{value} 次'
                }
            }
        ],
        series: []
    };
    var actionChart = echarts.init(document.getElementById('actionChart'));
    actionChart.setOption(actionChartOption);
    $.get("/api/agt_user_tag/test1?userId="+request.id).done(function (resp) {
        var legendDateArr = [], xAxisDataArr = [], seriesArr = [];
        legendDateArr = resp.legendData;
        xAxisDataArr = resp.xAxisData;
        seriesArr = resp.series;

        actionChart.setOption({
            legend: {data: legendDateArr},
            xAxis: {data: xAxisDataArr},
            series: seriesArr
        });

    });


    function getDateTime(date) {
        var year = date.getFullYear();
        var month = (date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
        var day = date.getDate() >= 10 ? date.getDate() : "0" + date.getDate();
        var hour = date.getHours() >= 10 ? date.getHours() : "0" + date.getHours();
        var minutes = date.getMinutes() >= 10 ? date.getMinutes() : "0" + date.getMinutes();
        var seconds = date.getSeconds() >= 10 ? date.getSeconds() : "0" + date.getSeconds();
        var dateStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds + ' ';
        return dateStr;
    }
    
    function getLatestDay(days) {
        //设置日期，当前日期的前七天
        var myDate = new Date(); //获取今天日期
        myDate.setDate(myDate.getDate() - days - 1);
        var dateArray = [];
        var dateTemp;
        var flag = 1;
        for (var i = 0; i < days; i++) {
            var month = (myDate.getMonth() + 1) >= 10 ? (myDate.getMonth() + 1) : "0" + (myDate.getMonth() + 1);
            var day = myDate.getDate() >= 10 ? myDate.getDate() : "0" + myDate.getDate();
            dateTemp = month + "-" + day;
            dateArray.push(dateTemp);
            myDate.setDate(myDate.getDate() + flag);
        }
        return dateArray;
    }

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});